package com.szgr.baoyin;

import android.app.Application;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.LruCache;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.OnStartTraceListener;
import com.baidu.trace.Trace;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.szgr.baoyin.model.City;
import com.szgr.baoyin.model.Datas;
import com.szgr.baoyin.model.ForumCategory;
import com.szgr.baoyin.model.Goods;
import com.szgr.baoyin.model.HomePageImage;
import com.szgr.baoyin.model.HomeTool;
import com.szgr.baoyin.model.Product;
import com.szgr.baoyin.model.ReduceGoods;
import com.szgr.baoyin.utils.MyTool;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jimbo on 2015/9/16.
 */
public class MyApplication extends Application {
    //登录的是客户端还是服务端
    private boolean isClient = true;

    //初始化所有的城市，供服务站选择
    private static List<City> allCity;
    private boolean isLogin;
    private String username;
    private String userPhone;
    private String password;
    private String usericon;
    private String key;
    //商城缓存数据
    private Datas datas;

    private String token;
    private String secret;

    //以下是volley所必须的初始化
    public static final String TAG = MyApplication.class.getSimpleName();

    private RequestQueue reqQueue;

    private static MyApplication mInstance;

    private List<Product> cart;

    private int cartNum;

    private List<HomePageImage> homePageImages;

    private ArrayList<ReduceGoods> specialImageUrls;
    private ArrayList<HomeTool.HomeToolItem> machineImageUrls;

    private List<ForumCategory> categories;

    public boolean isClient() {
        return isClient;
    }

    public void setIsClient(boolean isClient) {
        this.isClient = isClient;
    }

    public static List<City> getAllCity() {
        return allCity;
    }

    public static void setAllCity(List<City> allCity) {
        MyApplication.allCity = allCity;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        SDKInitializer.initialize(this);

        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=56049f3d");

        mInstance = this;
        getRequestQueue();

        MyTool.initContext(getApplicationContext());

        PushAgent mPushAgent = PushAgent.getInstance(getApplicationContext());
        mPushAgent.enable();

        String device_token = UmengRegistrar.getRegistrationId(getApplicationContext());
        System.out.println("---token---->" + device_token);



    }



    public static synchronized MyApplication getmInstance(){
        return  mInstance;
    }

    public RequestQueue getRequestQueue(){
        if(reqQueue == null){
            reqQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return reqQueue;
    }

    public <T> void addToRequestQueue(Request<T> req,String tag){
        req.setTag(TextUtils.isEmpty(tag)?TAG:tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req){
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag){
        if(reqQueue!=null){
            reqQueue.cancelAll(tag);
        }
    }

    private ImageLoader imageLoader;

    public ImageLoader getImageLoader(){
        getRequestQueue();
        if(imageLoader == null){
            imageLoader = new ImageLoader(this.reqQueue,new LruBitmapCache());
        }
        return this.imageLoader;
    }

    public boolean isLogin() {
        return isLogin;
    }

    public void setIsLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void clearLoginMessage() {
        username = null;
        usericon = null;
        password = null;
        isLogin = false;
        key = null;
        MyTool.saveSP("username","");
        MyTool.saveSP("password","");
        cartNum = 0;
        token = null;
        secret = null;
    }

    public List<Product> getCart() {
        return cart;
    }

    public void setCart(List<Product> cart) {
        this.cart = cart;
    }

    public int getCartNum() {
        return cartNum;
    }

    public void setCartNum(int cartNum) {
        this.cartNum = cartNum;
    }

    public List<HomePageImage> getHomePageImages() {
        return homePageImages;
    }

    public void setHomePageImages(List<HomePageImage> homePageImages) {
        this.homePageImages = homePageImages;
    }

    public ArrayList<ReduceGoods> getSpecialImageUrls() {
        return specialImageUrls;
    }

    public void setSpecialImageUrls(ArrayList<ReduceGoods> specialImageUrls) {
        this.specialImageUrls = specialImageUrls;
    }

    public ArrayList<HomeTool.HomeToolItem> getMachineImageUrls() {
        return machineImageUrls;
    }

    public void setMachineImageUrls(ArrayList<HomeTool.HomeToolItem> machineImageUrls) {
        this.machineImageUrls = machineImageUrls;
    }

    public String getUsericon() {
        return usericon;
    }

    public void setUsericon(String usericon) {
        this.usericon = usericon;
    }

    public List<ForumCategory> getCategories() {
        return categories;
    }

    public void setCategories(List<ForumCategory> categories) {
        this.categories = categories;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getUserPhone() {
        return userPhone;
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public static class LruBitmapCache extends LruCache<String,Bitmap> implements ImageLoader.ImageCache {
        public static int getDefaultLruBitmapCacheSize(){
            final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
            final int cacheSize=maxMemory/8;
            return cacheSize;
        }

        public LruBitmapCache(){
            this(getDefaultLruBitmapCacheSize());
        }

        public LruBitmapCache(int sizeInKiloBytes){
            super(sizeInKiloBytes);
        }

        @Override
        public int sizeOf(String key,Bitmap value){
            return value.getRowBytes()*value.getHeight()/1024;
        }

        @Override
        public Bitmap getBitmap(String url){
            return get(url);
        }

        @Override
        public void putBitmap(String url,Bitmap bitmap){
            put(url,bitmap);
        }
    }

    public Datas getDatas() {
        return datas;
    }

    public void setDatas(Datas datas) {
        this.datas = datas;
    }
}
